﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Export.Ui.ViewModel
{
    using DatabaseCore;
    using FirstFloor.ModernUI.Presentation;
    using GalaSoft.MvvmLight;
    using Export.Ui.ViewModel.Admin;
    using SystemCore;

    public class MainWindowViewModel : NotifyPropertyChanged
    {
        private LoginViewModel _loginViewModel;
        private HomeViewModel _homeViewModel;
        private QueueViewModel _queueViewModel;
        private AdminViewModel _adminViewModel;
        private UserMaintananceViewModel _userMaintanance;
        private OrderListViewModel _orderListViewModel;
        private Uri _homeUri;
       

        public MainWindowViewModel()
        {
            MenuLinkGroups = new LinkGroupCollection();
            TitleLink = new LinkCollection();
            _loginViewModel = new LoginViewModel(this, new UserDao());
            _homeViewModel = new HomeViewModel(this);
            _orderListViewModel = new OrderListViewModel(this);
            //_queueViewModel = new QueueViewModel(this);
            CreateMenu();
        }

        public LinkGroupCollection MenuLinkGroups { get; private set; }

        public LinkCollection TitleLink { get; private set; }

        public LoginViewModel LoginViewModel
        {
            get
            {
                return _loginViewModel;
            }
        }

        public HomeViewModel HomeViewModel
        {
            get
            {
                return _homeViewModel;
            }
        }

        public QueueViewModel QueueViewModel
        {
            get
            {
                return _queueViewModel;
            }
        }

        public AdminViewModel AdminViewModel
        {
            get
            {
                return _adminViewModel;
            }
        }

        public UserMaintananceViewModel UserMaintanance
        {
            get
            {
                return _userMaintanance;
            }
        }

        public OrderListViewModel OrderListViewModel
        {
            get
            {
                return _orderListViewModel;
            }
        }

        public UserContext UserContext
        {
            get
            {
                return _loginViewModel.getUserContext();
            }
        }

        public Uri HomeUri
        {
            get
            {
                return _homeUri;
            }

            set
            {
                _homeUri = value;
                OnPropertyChanged("HomeUri");
            }
        }

        private void CreateMenu()
        {
            MenuLinkGroups.Clear();
            
                var MenuGroup = new LinkGroup { DisplayName = "Menu", GroupName = "Page" };
                //var homeLink = new Link { DisplayName = "Page", Source = new Uri("/Pages/Home.xaml", UriKind.Relative) };
                //MenuGroup.Links.Add(homeLink);
                
                MenuLinkGroups.Add(MenuGroup);
                UpdateMenuSelection();
        }

        public void UpdateMenuSelection()
        {
            if (UserContext != null)
            {
                MenuLinkGroups.Clear();
                TitleLink.Clear();
                
                //create title link
                var LoggedTitleLink = new Link { DisplayName = "Logout", Source = new Uri("/Pages/Login.xaml", UriKind.Relative) };
                var SettingTitleLink = new Link { DisplayName = "Settings", Source = new Uri("/Pages/Settings.xaml", UriKind.Relative) };
                TitleLink.Add(LoggedTitleLink);
                TitleLink.Add(SettingTitleLink);
                var MenuGroup = new LinkGroup { DisplayName = "Menu", GroupName = "Menu" };
                switch ((ERole)UserContext.Role)
                {
                    case SystemCore.ERole.Admin:
                        HomeUri = new Uri("/Pages/AdminView.xaml", UriKind.Relative);
                        _adminViewModel = new AdminViewModel(this);
                        _userMaintanance = new UserMaintananceViewModel(this);
                        var adminLink = new Link { DisplayName = "Admin Section", Source = new Uri("/Pages/AdminView.xaml", UriKind.Relative) };
                        MenuGroup.Links.Add(adminLink);
                        MenuLinkGroups.Add(MenuGroup);
                        break;
                    case SystemCore.ERole.Approver:
                    case SystemCore.ERole.SC:
                        HomeUri = new Uri("/Pages/OrderList.xaml", UriKind.Relative);
                        var orderLink = new Link { DisplayName = "Order", Source = new Uri("/Pages/OrderList.xaml", UriKind.Relative) };
                        var queueLink = new Link { DisplayName = "Queue", Source = new Uri("/Pages/QueueView.xaml", UriKind.Relative) };

                        MenuGroup.Links.Add(orderLink);
                        MenuGroup.Links.Add(queueLink);
                        MenuLinkGroups.Add(MenuGroup);
                        
                        break;

                    default:
                        break;
                }   
                if (UserContext.Role == 1)
                {
                    
                    
                }

            }
            else
            {
                HomeUri = new Uri("/Pages/Login.xaml", UriKind.Relative);
            }
        }


    }
}
